import { Module } from 'vuex';
import { IRootState } from '@/store/type';
import { ISystemType } from './type';
import { getPageListData, deletePageList } from '@/service/main/system/system';
import localCache from '@/utils/localCache';
import type { IContentUrlType } from './type';
import { ElMessage, ElMessageBox } from 'element-plus';
import 'element-plus/es/components/message/style/css';
import 'element-plus/es/components/message-box/style/css';
const systemModule: Module<ISystemType, IRootState> = {
  namespaced: true,
  state() {
    return {
      userlist: localCache.getCache('usersList') ?? [],
      userCount: localCache.getCache('usersCount') ?? 0,
      rolelist: localCache.getCache('roleList') ?? [],
      roleCount: localCache.getCache('roleCount') ?? 0,
      goodslist: localCache.getCache('goodsList') ?? [],
      goodsCount: localCache.getCache('goodsCount') ?? 0,
      menulist: localCache.getCache('menuList') ?? []
    };
  },
  getters: {
    pageListData(state) {
      return (pathName: any) => {
        switch (pathName) {
          case 'users':
            return state.userlist;
            break;
          case 'role':
            return state.rolelist;
            break;
          case 'goods':
            return state.goodslist;
            break;
          case 'menu':
            return state.menulist;
            break;
        }
      };
    },
    pageCountData(state) {
      return (pathName: any) => {
        switch (pathName) {
          case 'users':
            return state.userCount;
            break;
          case 'role':
            return state.roleCount;
            break;
          case 'goods':
            return state.goodsCount;
            break;
        }
      };
    }
  },
  mutations: {
    changeusersList(state, payload) {
      state.userlist = payload;
    },
    changeusersCount(state, payload) {
      state.userCount = payload;
    },
    changeroleList(state, payload) {
      state.rolelist = payload;
    },
    changeroleCount(state, payload) {
      state.roleCount = payload;
    },
    changegoodsList(state, payload) {
      state.goodslist = payload;
    },
    changegoodsCount(state, payload) {
      state.goodsCount = payload;
    },
    changemenuList(state, payload) {
      state.goodslist = payload;
    },
    changemenuCount(state) {
      console.log('获取菜单管理数据成功');
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: IContentUrlType) {
      const { pagePath } = payload;
      const pageUrl = `/${pagePath}/list`;
      const pageResult = await getPageListData(pageUrl, payload.queryInfo);
      const { list, totalCount } = pageResult.data;
      commit(`change${pagePath}List`, list);
      localCache.setCache(`${pagePath}List`, list);
      commit(`change${pagePath}Count`, totalCount);
      localCache.setCache(`${pagePath}Count`, totalCount);
    },
    /* 删除信息 */
    async deletePageListAction({ commit, dispatch }, payload) {
      const { pagePath, id } = payload;
      const pageurl = `/${pagePath}/${id}`;
      //删除操作
      deletePageList(pageurl).then((res) => {
        ElMessageBox.alert('删除成功', '消息提示', {
          confirmButtonText: 'OK',
          customStyle: { width: '300px' },
          callback: () => {
            ElMessage({
              type: 'success',
              message: `已重新获取最新列表`,
              duration: 2000,
              center: true
            });
          }
        });
      });
      await deletePageList(pageurl);
      //删除成功后重新请求
      dispatch('getPageListAction', {
        pagePath,
        queryInfo: {
          size: 10,
          offset: 0
        }
      });
    }
  }
};
export default systemModule;
